package com.lc.rongzecaifu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;

import com.lc.rongzecaifu.MyApplication;
import com.lc.rongzecaifu.adapter.BankListAdapter2;
import com.lc.rongzecaifu.conn.JsonAccessToken;
import com.lc.rongzecaifu.conn.JsonBanklist;
import com.lc.rongzecaifu.conn.JsonBanknamelist;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

public class ChoseBankActivity extends BaseActivity {

    private BankListAdapter2 mAdapter;
    @BoundView(R.id.rv_01) private RecyclerView rv01;
    private List<JsonBanklist.Info.ListContent> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chose_bank);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);

        rv01.setLayoutManager(mLayoutManager);

        mAdapter = new BankListAdapter2(this, list);

        rv01.setAdapter(mAdapter);

        mAdapter.setOnItemCliclListener(new BankListAdapter2.OnItemClickListener() {
            @Override
            public void onClick(int position) {

                Intent intent = new Intent(ChoseBankActivity.this, ChoseBankActivity.class);

                intent.putExtra("id", list.get(position).id);
                intent.putExtra("name", list.get(position).bank_name);

                Log.e(list.get(0).bank_name, "66666666666666666666666");

                intent.putExtra("ico", list.get(position).bank_ico);

                if (!TextUtils.isEmpty(list.get(position).bank_num)) {
                    intent.putExtra("num", list.get(position).bank_num);
                }

                setResult(RESULT_OK, intent);

                finish();
            }
        });


        if (getIntent().getIntExtra("mode", 0) == 1) {

            // 这个是现示 全部的可选的列表
            getBankAccessToken();

        } else {

            // 这个是现示 你已经添加的银行卡列表
            getBankCardAccessToken();
        }
    }

    private void getBankAccessToken() {

        new JsonAccessToken("user/bank_name_list", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {

                new JsonBanknamelist(info.access_token, MyApplication.myPreferences.readUid(), new AsyCallBack<JsonBanknamelist.Info>() {
                    @Override
                    public void onSuccess(String toast, int type, JsonBanknamelist.Info info) throws Exception {

                        list.addAll(info.list);
                        mAdapter.notifyDataSetChanged();

                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {

                        UtilToast.show(ChoseBankActivity.this, toast);

                    }

                }).execute(ChoseBankActivity.this, true);

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                UtilToast.show(ChoseBankActivity.this, "安全验证失败！");
            }

        }).execute(this, true);

    }

    private void getBankCardAccessToken() {

        new JsonAccessToken("user/banklist", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {

                new JsonBanklist(info.access_token, MyApplication.myPreferences.readUid(), new AsyCallBack<JsonBanklist.Info>() {
                    @Override
                    public void onSuccess(String toast, int type, JsonBanklist.Info info) throws Exception {

                        list.addAll(info.list);
                        mAdapter.notifyDataSetChanged();

                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {

                        UtilToast.show(ChoseBankActivity.this, JsonBanklist.TOAST);

                    }

                }).execute(ChoseBankActivity.this, true);

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                UtilToast.show(ChoseBankActivity.this, "安全验证失败！");
            }

        }).execute(this, true);

    }



}
